package com.michale.admin.controller.monitor;

import com.michale.common.annotation.Log;
import com.michale.common.constants.RedisCacheConstant;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.utils.security.SecurityUtils;
import com.michale.system.service.CacheService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author Michale
 * @apiNote 缓存管理
 */
@Api(tags = "缓存管理")
@RestController
@RequestMapping("/monitor/cache")
public class CacheController {
    @Resource
    private CacheService cacheService;

    /**
     * 查询缓存名称列表
     *
     * @return 缓存名称列表
     */
    @GetMapping("/cacheNameList")
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult getCacheNameList() {
        return AjaxResult.success(cacheService.getCacheNameList());
    }

    /**
     * 查询缓存键名列表
     *
     * @param cacheName 缓存名称
     * @return 缓存键名列表
     */
    @GetMapping("/cacheKeyList/{cacheName}")
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult cacheKeyList(@PathVariable String cacheName) {
        Set<String> cacheKeys = cacheService.getCacheKeyList(cacheName);
        return AjaxResult.success(cacheKeys);
    }

    /**
     * 查询缓存内容
     *
     * @param cacheName 缓存名称
     * @param cacheKey  缓存键值
     * @return 缓存内容
     */
    @GetMapping("/getValue/{cacheName}/{cacheKey}")
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey) {
        return AjaxResult.success(cacheService.getCacheValue(cacheName, cacheKey));
    }

    /**
     * 清理指定名称缓存
     *
     * @param cacheName 缓存名称
     * @return 结果
     */
    @DeleteMapping("/clearCacheName/{cacheName}")
    @Log(title = "清理缓存", businessType = BusinessType.CLEAN)
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult clearCacheName(@PathVariable String cacheName) {
        cacheService.clearCacheName(cacheName);
        return AjaxResult.success();
    }

    /**
     * 清理指定键名缓存
     *
     * @param cacheKey 缓存键名
     * @return 结果
     */
    @DeleteMapping("/clearCacheKey/{cacheKey}")
    @Log(title = "清理缓存", businessType = BusinessType.CLEAN)
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult clearCacheKey(@PathVariable String cacheKey) {
        cacheService.clearCacheKey(RedisCacheConstant.LOGIN_KEY + SecurityUtils.getLoginUser().getUsername());
        cacheService.clearCacheKey(cacheKey);
        return AjaxResult.success();
    }

    /**
     * 根据缓存名称清理全部缓存键名
     *
     * @param cacheName 缓存名称
     * @return 结果
     */
    @DeleteMapping("/clearCacheKeyAll")
    @Log(title = "清理缓存", businessType = BusinessType.CLEAN)
    @PreAuthorize("@security.isPermission('monitor:cache:list')")
    public AjaxResult clearCacheKeyAll(String cacheName) {
        cacheService.clearCacheKeyAll(cacheName);
        return AjaxResult.success();
    }
}
